package com.wontube.smr.remote;

import java.util.ArrayList;
import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.LocationBO;
import com.wontube.smr.client.IDeviceHandler;
import com.wontube.smr.client.model.po.Country;
import com.wontube.smr.client.model.po.DeviceExpert;
import com.wontube.smr.client.model.po.WebsiteLink;
import com.wontube.smr.client.model.vo.LocationInfo;
import com.wontube.smr.dao.bo.CountryDao;
import com.wontube.smr.dao.bo.DeviceExportDao;
import com.wontube.smr.dao.bo.WebsiteCategoryDao;
import com.wontube.smr.dao.bo.WebsiteLinkSelectDao;


/**
 * @author yuechao
 *
 */
@NoLoginSupport
public class DeviceHandler extends BasicHandler implements IDeviceHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<DeviceExpert> getAllSimulateAgent() {
		List<DeviceExpert> deList = DeviceExportDao.getAllExpert();
		return deList;
	}
	
	/* 
	 * @see smr.client.IDeviceHandler#getSimulateAgent(java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public String getSimulateAgent(String domain, String sysId) {
		DeviceExpert expert = DeviceExportDao.getExpert(domain, sysId);
		if (expert == null) {
			return "";
		}
		
		return expert.getSimulateAgent();
	}
	
	@Override
	public LocationInfo getLocation() {
		String ip = super.getContext().getSession().getLastClientIP();
		LocationInfo location = null;
		try {
			location = LocationBO.getLocation(ip);
		} catch (Exception e) {
			location = null;
		}
		
		if (location == null) {
			throw new RuntimeException("failed to get location");
		}
		
		return location;
	}

	/* 
	 * @see smr.client.IDeviceHandler#isDownSupport(java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Boolean isDownSupport(String domain, String sysId) {
		DeviceExpert expert = DeviceExportDao.getExpert(domain, sysId);
		if (expert == null) {
			return Boolean.FALSE;
		}
		
		return expert.getDownSupport() != 0;
	}
	
	/*
	 * @see com.wontube.smr.client.IDeviceHandler#listAllCategory()
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<String> listAllCategory() {
		return WebsiteCategoryDao.listAllCategory("US");
	}
	
	/* 
	 * @see com.wontube.smr.client.IDeviceHandler#listAllCategory(java.lang.String)
	 */
	@Override
	public List<String> listAllCategory(String countryCode) {
		return WebsiteCategoryDao.listAllCategory(countryCode);
	}
	
	/* 
	 * @see com.wontube.smr.client.IDeviceHandler#listDeviceHomeLinks(java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listDeviceHomeLinks(String sysId) {
		if (StringUtils.isNullOrEmpty(sysId)) {
			return new ArrayList<WebsiteLink>();
		}
		return WebsiteLinkSelectDao.listDeviceHome(sysId, "US");
	}

	/* 
	 * @see com.wontube.smr.client.IDeviceHandler#listMoreLinks(java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listMoreLinks(String sysId) {
		if (StringUtils.isNullOrEmpty(sysId)) {
			return new ArrayList<WebsiteLink>();
		}
		return WebsiteLinkSelectDao.listMoreItems(sysId, "US");
	}
	
	/* 
	 * @see smr.client.IDeviceHandler#listDeviceHomeLinks()
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listDeviceHomeLinks() {
		return this.listDeviceHomeLinks(IDeviceHandler.sysIdAndroid);
	}

	/* 
	 * @see smr.client.IDeviceHandler#listMoreLinks()
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listMoreLinks() {
		return this.listMoreLinks(IDeviceHandler.sysIdAndroid);
	}

	/*
	 * @see com.wontube.smr.client.IDeviceHandler#listDeviceHomeLinks(java.lang.String, java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listDeviceHomeLinks(String sysId, String countryCode) {
		if (StringUtils.isNullOrEmpty(sysId) || StringUtils.isNullOrEmpty(countryCode)) {
			return new ArrayList<WebsiteLink>();
		}
		return WebsiteLinkSelectDao.listDeviceHome(sysId, countryCode);
	}

	/*
	 * @see com.wontube.smr.client.IDeviceHandler#listMoreLinks(java.lang.String, java.lang.String)
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<WebsiteLink> listMoreLinks(String sysId, String countryCode) {
		if (StringUtils.isNullOrEmpty(sysId) || StringUtils.isNullOrEmpty(countryCode)) {
			return new ArrayList<WebsiteLink>();
		}
		return WebsiteLinkSelectDao.listMoreItems(sysId, countryCode);
	}

	/*
	 * @see com.wontube.smr.client.IDeviceHandler#listCountries()
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<Country> listCountries() {
		return CountryDao.listCountries();
	}
}
